﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PagedList;
using ThesisSystem.Models;
using ThesisSystem.DAL;
using ThesisSystem.ViewModels;


namespace ThesisSystem.Controllers
{ 
    [Authorize (Roles="Administrators")]
    public class JudgeController : Controller
    {
        private ThesisSystemEntities db = new ThesisSystemEntities();

        #region Index & Details
        //
        // GET: /Judge/

        public ViewResult Index()
        {
            return View(db.Targets.ToList());
        }

        //
        // GET: /Judge/Details/5

        public ViewResult Details(int id)
        {
            Target target = db.Targets.Find(id);
            return View(target);
        }
        #endregion
        #region 添加指标
       
        //
        // GET: /Judge/Create

        public ActionResult Create()
        {

            ViewBag.TypeName = new SelectList(db.ThesisTypes, "TypeName", "TypeName"); 
            var Weight = new List<string>()
            {
                "1","2","3","4","5","6","7","8","9","10"
            };
            ViewBag.weight = new SelectList(Weight, null, null);
            return View();
        } 

        //
        // POST: /Judge/Create

        [HttpPost]
        public ActionResult Create(Target target)
        {
            if (ModelState.IsValid)
            {
                db.Targets.Add(target);
                db.SaveChanges();
                
                return RedirectToAction("Index");  
            }

            return View(target);
        }
         #endregion

        #region 指标查询
        //
        //GET:Judge/Search/4
        public ActionResult Search(int? page,TargetsSearch targetsSearch)
        {
            DbModelBuilder modelBuilder = new DbModelBuilder();
            modelBuilder.Entity<Target>()
                .HasMany(e => e.Markings)
              .WithMany(e => e.Targets)
                .Map(m =>
                {
                    m.ToTable("MarkingTargets");
                    m.MapLeftKey("MarkingID");
                    m.MapRightKey("TargetID");
                }); 
            var targets = db.Targets
                        .Include(t => t.Markings).ToList();

          
            ViewBag.TypeName = new SelectList(db.ThesisTypes, "TypeName", "TypeName");

            //根据指标概括关键字
            if (!string.IsNullOrEmpty(targetsSearch.Summary))
            {
              
                targets = targets.Where(t => t.Summary.ToUpper().Contains(targetsSearch.Summary.ToUpper())).ToList();
            }
            //根据指标适用学科
            if (!string.IsNullOrEmpty(targetsSearch.Typename))
            {
                targets = targets.Where(t => t.TypeName.ToUpper().Contains(targetsSearch.Typename.ToUpper())).ToList();
            }
            //根据指标权重
            if (!string.IsNullOrEmpty(targetsSearch.Weight))
            {
                targets = targets.Where(t => t.Weight.ToString() == targetsSearch.Weight.ToString()).ToList();
            }
            //根据所属评卷
            if (!string.IsNullOrEmpty(targetsSearch.Title))
            {
                foreach (var target in targets)
                {
                    target.Markings = target.Markings.Where(m => m.Title.ToUpper().Contains(targetsSearch.Title)).ToList();
                }
            }

            ViewBag.Current = targetsSearch;
            int pageSize =3;//定义每页显示的记录数
            int pageIndex = (page ?? 1) - 1;
            //return View( targets.topToPagedList(pageIndex, pageSize));
            return View(targets.ToPagedList(pageIndex, pageSize));
            
        }
        //
        //


        #endregion

        #region 指标编辑
        //
        // GET: /Judge/Edit/5
 
        public ActionResult Edit(int id)
        {
            Target target = db.Targets.Find(id);

            ViewBag.TypeName = new SelectList(db.ThesisTypes, "TypeName", "TypeName");
            var Weight = new List<string>()
            {
                "1","2","3","4","5","6","7","8","9","10"
            };
            ViewBag.weight = new SelectList(Weight,null,null);
            return View(target);
        }

        //
        // POST: /Judge/Edit/5

        [HttpPost]
        public ActionResult Edit(Target target)
        {
            if (ModelState.IsValid)
            {
                db.Entry(target).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(target);
        }
        #endregion

        #region 指标删除

        //
        // GET: /Judge/Delete/5
 
        public ActionResult Delete(int id)
        {
            Target target = db.Targets.Find(id);
            return View(target);
        }

        //
        // POST: /Judge/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            Target target = db.Targets.Find(id);
            db.Targets.Remove(target);
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        #endregion
        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}